package com.lzx.spring3.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Random;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.lzx.spring3.annotation.Cache;
import com.lzx.spring3.dao.UserDao;
import com.lzx.spring3.meta.User;

@Component("userDao")
public class UserDaoImpl extends BaseDao implements UserDao {

	@Override
	@Cache
	public int add(User user) {
//		String sql = "insert into user (name, age) values (?, ?)";
//		return jdbcTemplate.update(sql,
//				new Object[] { user.getName(), user.getAge() });
		return 0;
	}

	@Override
	public int deleteAll() {
		String sql = "delete from user";
		return jdbcTemplate.update(sql);
	}

	@Override
	public List<User> getUserList() {
		String sql = "select * from user";
		return jdbcTemplate.query(sql, new RowMapper<User>() {

			@Override
			public User mapRow(ResultSet rs, int arg1) throws SQLException {
				User user = new User();
				user.setName(rs.getString("name"));
				user.setAge(rs.getInt("age"));
				return user;
			}

		});
	}

	@Override
	public int update(User t) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	@Cache
	public int delete(User t) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	@Cache
	public Object get1(User user) {
		System.out.println("invoke get1(User user)");
		new Random().nextDouble();
		return "get1";
	}

	@Override
	@Cache
	public Object get2() {
		// TODO Auto-generated method stub
		new Random().nextDouble();
		return "get2";
	}

}
